import api from '@api/store_api';
import { toast, err } from '@utils/wxapi';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    item: Object,
  },

  /**
   * 组件的初始数据
   */
  data: {},

  /**
   * 组件的方法列表
   */
  methods: {
    async change(e) {
      try {
        const { item } = e.currentTarget.dataset;
        const goodsCount = e.detail;
        if (goodsCount === 0) {
          await this.deleteItem(item.id);
          this.triggerEvent('cart');
        } else {
          await this.updateItem(item.id, goodsCount);
          this.triggerEvent('cart');
        }
      } catch (error) {
        err(error);
        throw error;
      }
    },
    async deleteItem(itemId) {
      const res = await api.eletcai({ id: [itemId] });
      if (res.respCode !== 200) {
        toast(res.msg);
      }
    },
    async updateItem(id, goodsCount) {
      const res = await api.editcart({ id, goodsCount });
      if (res.respCode !== 200) {
        toast('修改失败请稍后重试');
      }
    },
  },
});
